<?php

namespace app\model\v1\recovery;

use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Exception;
use think\Model;


class RecoveryQuestionType extends Model
{
    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];



    /**
     * 配置类型管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getQuestionTypeList($param = [])
    {
        $order = ["id"=>"desc"];

        $data['data'] = self::field("*,FROM_UNIXTIME(create_time) as create_time")->whereNull("delete_time")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                //根据关键字搜索
                if(!empty($param["word"])) $query->where("type_name",'like', '%'.$param["word"].'%');
            })
            ->order($order)
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data['count'] = self::whereNull("delete_time")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("type_name",'like', '%'.$param["word"].'%');
            })
            ->count();

        return $data;
    }


    /**
     * 配置类型管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addQuestionType($param = [])
    {
        $find = self::where("type", $param["type"])->whereNull("delete_time")->find();
        if(!empty($find)) Throw new Exception('type重复添加!');

        $add = [
            "admin_id"      => $param["admin_id"],  //创建人id
            "type_name"     => $param["type_name"] ?? '', //类型名称
            "type"          => $param["type"],  //类型
            "create_time"   => time(),
        ];
        return self::insert($add);
    }


    /**
     * 配置类型管理列表 - 编辑
     * @param $param
     * @return RecoveryQuestionType
     */
    public static function updateQuestionType($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["type_name"]))     $update["type_name"] = $param["type_name"];  //分类名称
        if(!empty($param["type"]))          $update["type"] = $param["type"]; //类型

        return self::where("id", $param["id"])->update($update);
    }



    /**
     * 配置类型管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableQuestionTypeStatus($param = [])
    {
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }





}